package com.zzw.mvvm.viewmodels.main;

import android.annotation.SuppressLint;
import android.util.Log;

import com.zzw.mvvm.base.BaseViewModel;
import com.zzw.mvvm.beans.Demo;
import com.zzw.mvvm.repository.net.HttpRequest;
import com.zzw.mvvm.utils.rxutils.RxEventManager;
import com.zzw.mvvm.utils.rxutils.RxUtil;

import java.util.concurrent.TimeUnit;

import androidx.lifecycle.MutableLiveData;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainViewModel extends BaseViewModel {
    MutableLiveData<String> name;
    public MutableLiveData<String> getName() {
        if (name == null) {
            name = new MutableLiveData<>();
            name.setValue("zz");
        }
        return name;
    }

    public void setName(MutableLiveData<String> name) {
        this.name = name;
    }

    @SuppressLint("CheckResult")
    public void changeName() {
        Observable.interval(1, 1, TimeUnit.SECONDS).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        Log.i("zzw", "along" + aLong);
                        name.setValue("zz-" + aLong);
                    }
                });
    }

    @SuppressLint("CheckResult")
    public void getNetMsg() {
        HttpRequest.getApiService().getUser("yuantong", "11111111111")
                .compose(RxUtil.rxSchedulerHelper())
                //.subscribeOn(Schedulers.io())
                .subscribe(new Consumer<Demo>() {
                    @Override
                    public void accept(Demo demo) throws Exception {
                        Log.i("zzw", demo.getMessage());
                        name.setValue("" + demo.getMessage());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e("zzw",throwable.getMessage());
                    }
                });
    }
}
